/**
 * 
 */
package calculation.passed;

import java.util.ArrayList;

/**
 * @author xyyi
 *
 */
public class PlusOne {

	/**
	 * Given a number represented as an array of digits, plus one to the number.
	 */
	public int[] plusOne(int[] digits) {
		// Start typing your Java solution below
		// DO NOT write main() function
		ArrayList<Integer> retList = new ArrayList<Integer>(digits.length + 1);
		int carry = 1;
		for (int i = digits.length - 1, sum = 0; i >= 0; i--) {
			sum = digits[i] + carry;
			carry = sum / 10;
			retList.add(sum % 10);
		}

		// need to check carry after loop
		if (carry > 0)
			retList.add(carry);

		int[] result = new int[retList.size()];
		for (int i = 0, size = result.length; i < size; i++) {
			result[i] = retList.get(size - i - 1);
		}
		return result;
	}

	/**
	 * 
	 */
	public PlusOne() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
